package app.com.freshnmenapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import app.com.freshnmenapp.bean.MyBean;

public class MessagesDetailActivity extends AppCompatActivity {

    private TextView textView1;
    private TextView textView2;
    private TextView textView3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messages_detail);
        initView();
        initData();
        initEvent();
    }
    private void initView(){
        textView1 = findViewById(R.id.textview1);
        textView2 = findViewById(R.id.textview2);
        textView3 = findViewById(R.id.textview3);

    }
    private void initData(){
        getData();

    }
    private  void getData(){
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

        String url = " http://www.xhjjyst.com/freshmenapp/messages/findone ";

        JSONObject jsonObject = new JSONObject();

        try {

            jsonObject.put("id", 2);

        } catch (JSONException e) {

            e.printStackTrace();

        }



        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {

            @Override

            public void onResponse(JSONObject jsonObject) {

                Log.e("TAG", "onResponse: " + jsonObject);

                /*处理结果数据*/

                // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）

                Gson gson = new Gson();

                MyBean myBean = gson.fromJson(jsonObject.toString(), MyBean.class);



                if(myBean.isSuccess()){

                    Toast.makeText(MessagesDetailActivity.this, "成功", Toast.LENGTH_SHORT).show();

                    // 获取数据，
//                  List < MyBean.ListBean >list=MyBean.getList();

              MyBean.ListBean list = myBean.getList();

                 textView1.setText(list.getTitle()+"");
                    textView2.setText(list.getDate()+"");
                    textView2.setText(list.getContent()+"");


                    // 在控件上显示文字



                    // 在控件上显示图片



                }else{

                    Toast.makeText(MessagesDetailActivity.this, "失败", Toast.LENGTH_SHORT).show();

                }

            }



        }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError volleyError) {

                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();

            }

        });

        requestQueue.add(jsonObjectRequest);



    }
    private void initEvent(){

    }
}
